<template>
    <div class="login-container">
<!--登录盒子区域-->
      <div class="login_box">
<!--头像区域-->
        <div class="avatar_box">
          <img src="../assets/logo.png"/>
        </div>
<!--登录表单区域-->
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
            <!--用户名-->
          <el-form-item prop="username">
            <el-input v-model="loginForm.username"  prefix-icon="el-icon-user"></el-input>
          </el-form-item>
          <!--密码-->
          <el-form-item prop="password">
            <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password"></el-input>
          </el-form-item>
          <!--滑块验证区域-->
          <el-form-item>
            <el-row>
                <drag-verify
                  :circle="false"
                  ref="dragVerify"
                  text="请按住滑块拖动"
                  successText="验证通过"
                  handlerIcon="el-icon-d-arrow-right"
                  successIcon="el-icon-circle-check"
                  :width="410"
                  :height="45"
                  @passcallback="passCallback">
                </drag-verify>
            </el-row>
          </el-form-item>

          <!--按钮区域-->
          <el-form-item class="btns">
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="info" @click="resetLoginForm">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

    </div>
</template>

<script>
    import dragVerify from "vue-drag-verify";

    export default {
      data(){
        return{
          //登录表单数据对象
          loginForm:{
            username:'admin',
            password:'123456'
          },
          //登录表单验证规则对象
          loginFormRules:{
            username: [
              { required: true, message: '请输入用户名', trigger: 'blur' },
              { min: 3, max: 8, message: '用户名长度在 3 到 8 个字符', trigger: 'blur' }
            ],
            password:[
              { required: true, message: '请输入密码', trigger: 'blur' },
              { min: 6, max: 18, message: '密码长度在 6 到 18 个字符', trigger: 'blur' }
            ]
          }
        }
      },
      components:{
        //映射组件标签
        dragVerify
      },
      methods:{
        //点击重置按钮，重置表单
        resetLoginForm(){
          //重置表单
          this.$refs.loginFormRef.resetFields()
        },
      //  点击登录按钮，进行登录验证
        login(){
        //  发送请求前先进行表单数据的预校验
          this.$refs.loginFormRef.validate(  async valid=> {
            //判断 valid 的值为false则代表验证未通过，不发送登录请求
            if(!valid || !this.$refs.dragVerify.isPassing) {
              return this.$message.warning('请检查表单项，并滑动滑块')
            }

            //验证通过，发送登录请求
            const { data:res }=await this.$http.post('/login',this.loginForm)

            //判断请求是否成功
            if(res.meta.status !==200) {
              return this.$message({
                message:'登录失败！请检查用户名和密码是否正确~',
                type:'error'
              })
            }
            //请求成功
            this.$message({
              message:`欢迎${this.loginForm.username}登录`,
              type:'success',
              duration:1500
            })

            //保存用户登录的token
            window.sessionStorage.setItem('token',res.data.token)

            //通过编程式路由导航跳转到主页
            this.$router.push('/home')
          })
        },
        passCallback(){
          this.$message({
          message:'验证通过',
          type:'success',
          duration:800
        })
        }
      }
    }
</script>

  <style lang="less">
    .login-container{
      background: url("../assets/logo1.png");
      height: 100%;
      background-size: 100% 100%;
    }
    .login_box{
      width:450px;
      height:350px;
      background-color:#f2fcfe;
      opacity:0.8;
      border-radius:5px;
      position: absolute;
      left: 50%;
      top: 50%;
      transform: translate(-50%,-50%);

    /*  头像区域样式*/
      .avatar_box{
        width: 130px;
        height: 130px;
        border-radius: 50%;
        border: 1px solid #eee;
        position: absolute;
        left: 50%;
        transform: translate(-50%,-50%);

        img{
          width: 100%;
          height: 100%;
          border-radius:50%;
        }
      }
    }

    .login_form{
      box-sizing: border-box;
      position: absolute;
      bottom: 0;
      width: 100%;
      padding: 20px;
    }
    .btns{
      display: flex;
      justify-content: flex-end;
    }


  </style>
